// pages/index/index.js
const app = getApp();
Page({
  data: {
    statusBarHeight: 0,
    phone:'',
    pwd:'',
    sendMsgId : [
      "IFRjqaGJiQnItvi3lBnFgfHFSEewt_3BzAi6kjK_B3Q"
    ],
    userInfo:'',
    showModal:false,
    code:''
  },

  onLoad() {

    if (wx.getStorageSync('token')) {
      wx.reLaunch({ url: '/pages/home/home' })
    }
    this.getSystemInfo()
  },

  getSystemInfo() {
    const systemInfo = wx.getSystemInfoSync()
    this.setData({
      statusBarHeight: systemInfo.statusBarHeight
    })
  },

  // 登录
  handleAuthLogin(e) {
    let that = this

   wx.request({
     url: 'https://ylapp.zhongqueproperty.cn/manager/app/login',
     method:"POST",
     data:{"loginName":this.data.phone,"password":this.data.pwd},
     success: function(res) {
       debugger;
       console.log("----!----res="+res.data);
       console.log("----!----res="+res.data.code);
       if (res.data.code == 0) {
        
         console.log("----------1--------"+res.data.data.data.token);
         app.globalData.token = res.data.data.data.token;
         app.globalData.name = res.data.data.data.name;
         app.globalData.phone = res.data.data.data.phone;
         app.globalData.userInfo = res.data.data.data.employee;
         wx.setStorageSync("user_token",res.data.data.data.token);
         wx.setStorageSync("userInfo",res.data.data.data.employee);
         wx.showToast({ title: '登录成功' })
         debugger;
         //弹订阅
         uni.requestSubscribeMessage({
          tmplIds: this.sendMsgId, 
          success: (resMsg) => {
          console.log("提交订单消息推送",resMsg)
          if(this.sendMsgId && this.sendMsgId.length) {
            this.sendMsgId.forEach((item,index) => {
              if (resMsg[item] === 'accept') {
                // 用户同意订阅该模板
                console.log('提交订单用户同意订阅该模板', item);
              }
              if (resMsg[item] === 'reject') {
                // 用户拒绝订阅该模板
                console.log('提交订单用户拒绝订阅该模板', item);
              }
            })
          }
          },
          fail: (err) => {
          console.error('订阅消息失败', err);
          }
        });
         // 跳转到主页
         wx.navigateTo({ url: '/pages/home/home' })
       }
      
     }
   })
  },

  // 发送授权请求到后端
  sendAuthRequest(userInfo) {
    let that = this;
      wx.setStorageSync('userInfo', userInfo.detail.userInfo)
      wx.login({
        success: (res) => {
          if(res.code){
            that.setData({
              code:res.code
            });
            wx.request({
              url: 'https://ylapp.zhongqueproperty.cn/manager/app/login',
              method: 'POST',
              data: {
                code: that.data.code,
                encryptedData: userInfo.detail.encryptedData,
                iv: userInfo.detail.iv
              },
              success: res => {
             
                console.log("----!----res="+res.data);
                console.log("----!----res="+res.data.code);
                if (res.data.code == 0) {
                  wx.setStorageSync("user_token",res.data.data.data.token);
                  wx.setStorageSync("userInfo",res.data.data.data.employee);
                  wx.showToast({ title: '登录成功' })
                      //弹订阅
                      wx.showModal({
                        title: '消息订阅',
                        content: '是否接受设备消息订阅',
                        complete: (res) => {
                          if (res.cancel) {
                            wx.navigateTo({ url: '/pages/home/home' })
                          }
                      
                          if (res.confirm) {
                            console.log("++++++   "+that.data.sendMsgId)
                            wx.requestSubscribeMessage({
                              tmplIds: that.data.sendMsgId, 
                              success: (resMsg) => {
                              debugger;
                              console.log("提交订单消息推送",resMsg)
                                that.data.sendMsgId.forEach((item,index) => {
                                  if (resMsg[item] === 'accept') {
                                    // 用户同意订阅该模板
                                    console.log('提交订单用户同意订阅该模板', item);
                                    wx.navigateTo({ url: '/pages/home/home' })
                                  }
                                  if (resMsg[item] === 'reject') {
                                    // 用户拒绝订阅该模板
                                    console.log('提交订单用户拒绝订阅该模板', item);
                                    wx.navigateTo({ url: '/pages/home/home' })
                                  }
                                })
                              
                              },
                              fail: (err) => {
                              console.error('订阅消息失败', err);
                              wx.navigateTo({ url: '/pages/home/home' })
                              }
                            });
                          }
                        }
                      })
               
                 
                }
              }
            })
          }
          }
      })
    
  },
  // 体验登录
  handleExperienceLogin() {
    wx.showModal({
      title: '提示',
      content: '体验模式部分功能将受限',
      success(res) {
        if (res.confirm) {
          wx.switchTab({ url: '/pages/home/index' })
        }
      }
    })
  }
})